#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <assert.h>

#define FOO

#define bar() \
#ifdef FOO \
	printf("FOO defined!\n"); \
#else \
	printf("FOO not defined!\n"); \
#endif

void error(const char *msg)
{
	DWORD err, res;
	char buf[500];

	err = WSAGetLastError();
	res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
		NULL, err, 0, buf, sizeof(buf), NULL);
	if (res > 0)
		fprintf(stderr, "err: %d, %s\n", err, buf);
	else
		err = GetLastError();
    exit(0);
}

int main(int argc, char *argv[])
{
    int s;
    struct sockaddr_in addr;
//	struct timeval tv = {2, 0};
	int timeout = 3000;
	WSADATA wsadata;

	bar();

	if (argc != 3)
	{
		printf("usage: testcli <addr> <port>\n");
		return 1;
	}
	memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
	addr.sin_addr.s_addr = inet_addr(argv[1]);

	if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
		error("WSAStartup");
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s == INVALID_SOCKET)
		error("socket");
	if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout)) < 0)
		error("setsockopt 1");
	if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0)
		error("setsockopt 2");
    if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0)
		error("connect");
    if (send(s, "first line", 10, 0) != 10)
		error("send");
    Sleep(3000);
    if (send(s, "abcdefghijklmnop", 16, 0) != 16)
		error("send");
    if (send(s, "[foo]", 5, MSG_OOB) != 5)
		error("send");
    if (send(s, "abcdefghijklmnop", 16, 0) != 16)
		error("send");
    Sleep(3000);
    if (closesocket(s) < 0)
		error("close");
	WSACleanup();
    return 0;
}

